package modelo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SiniestroDAO {

	public SiniestroDAO() {
		super();
	}

	public void registrarSiniestro(Siniestro siniestro) {
		String tiraSQL = "INSERT INTO siniestro "+
		 				 "(sin_cod, sin_pol, sin_fec, sin_descp, sin_status) "+
		 				 "VALUES ("+
		 				 "'"+ siniestro.getSin_cod() + "','"
		 				 + siniestro.getSin_pol() + "','"
		 				 + siniestro.getSin_fec() + "','"
		 				 + siniestro.getSin_descp() + "','"
		 				 + siniestro.getSin_status() +"')";
		Conexion.ejecutar(tiraSQL);
	}
	
	
	
	public List<Siniestro> consultarSiniestro() {
		List<Siniestro> siniestros = new ArrayList<Siniestro>();
		String tiraSQL = "SELECT * FROM siniestro";
		ResultSet resultSet = Conexion.consultar(tiraSQL);		
		try {
			while (resultSet.next()) {
				
				String sin_cod = resultSet.getString("sin_cod");
				String sin_pol = resultSet.getString("sin_pol");
				String sin_fec = resultSet.getString("sin_fec");
				String sin_descp = resultSet.getString("sin_descp");
				String sin_status = resultSet.getString("sin_status");
				
				Siniestro siniestro = new Siniestro (sin_cod, sin_pol, sin_fec, sin_descp, sin_status);
				siniestros.add(siniestro);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return siniestros;
	}


	public Siniestro consultarExitenciaSiniestro(String cod) {
		Siniestro siniestro;
		String tiraSQL = "SELECT * FROM siniestro";
		ResultSet resultSet = Conexion.consultar(tiraSQL);		
		try {
			while (resultSet.next()) {
				
				String sin_cod = resultSet.getString("sin_cod");
				
				if (sin_cod.equals(cod)){
				
				String sin_pol = resultSet.getString("sin_pol");
				String sin_fec = resultSet.getString("sin_fec");
				String sin_descp = resultSet.getString("sin_descp");
				String sin_status = resultSet.getString("sin_status");
				
				siniestro = new Siniestro (sin_cod, sin_pol, sin_fec, sin_descp, sin_status);
				return siniestro;

				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	public String ultimoSiniestro() {
		String sin_cod = "";
		String tiraSQL = "SELECT * FROM siniestro";
		ResultSet resultSet = Conexion.consultar(tiraSQL);		
		try {
			while (resultSet.next()) {
				
			     sin_cod = resultSet.getString("sin_cod");
			}
				
		   } catch (SQLException e) {
			   e.printStackTrace();
		}
		return sin_cod;
	}

	public Siniestro ExitenciaSiniestroPoliza(String codigo,
			String poliza) {
			
		Siniestro siniestro;
		String tiraSQL = "SELECT * FROM siniestro";
		ResultSet resultSet = Conexion.consultar(tiraSQL);		
		try {
			while (resultSet.next()) {
				
				String sin_cod = resultSet.getString("sin_cod");
				String sin_pol = resultSet.getString("sin_pol");
				
				if (sin_cod.equals(codigo) && sin_pol.equals(poliza)){
				
				String sin_fec = resultSet.getString("sin_fec");
				String sin_descp = resultSet.getString("sin_descp");
				String sin_status = resultSet.getString("sin_status");
				
				siniestro = new Siniestro (sin_cod, sin_pol, sin_fec, sin_descp, sin_status);
				return siniestro;

				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}

//	
//	public void actualizarSiniestro(Siniestro siniestro) {
//
//		String codsiniestro = siniestro.getSin_cod();
//		String codpoliza = siniestro.getSin_pol();
//		
//		String tiraSQL = "UPDATE siniestro SET"+
//		 				 " sin_fec = '" + siniestro.getSin_fec() + 
//		 				 "', sin_descp = '" + siniestro.getSin_descp() +
//		 				 "', sin_status = '" + siniestro.getSin_status() +
//		 				 "' WHERE " + codsiniestro +" = '"+ siniestro.getSin_cod() +
//		 				 "' and " + codpoliza + " = '" + siniestro.;
//		System.out.println(tiraSQL);
//		Conexion.ejecutar(tiraSQL);
//	}
//	UPDATE siniestro
//	   SET sin_cod=?, sin_pol=?, sin_fec=?, sin_descp=?, sin_status=?
//	 WHERE <condition>;
//	
	

	public void actualizarSiniestro(Siniestro siniestro, String codigo, String pol ) {
			
		String codsiniestro = codigo;
		String codpoliza = pol;
		
		String tiraSQL = "SELECT * FROM siniestro";
		ResultSet resultSet = Conexion.consultar(tiraSQL);		
		try {
			while (resultSet.next()) {
				
				
				String sin_cod = resultSet.getString("sin_cod");
				String sin_pol = resultSet.getString("sin_pol");
			
				if (codsiniestro.equalsIgnoreCase(sin_cod) && codpoliza.equalsIgnoreCase(sin_pol)){
					
					String tiraSQLL = "UPDATE siniestro SET"+
			 				 " sin_fec = '" + siniestro.getSin_fec() + 
			 				 "', sin_descp = '" + siniestro.getSin_descp() +
			 				 "', sin_status = '" + siniestro.getSin_status() +
			 				 "' WHERE sin_cod = '"+ sin_cod +"' and sin_pol = '" +  sin_pol + "'";

					Conexion.ejecutar(tiraSQLL);
					break;
				}
								
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


//	DELETE FROM siniestro
//	 WHERE sin_cod = 'SEGSI1004';


	public void eliminarSiniestro(Siniestro siniestro, String codigo, String pol ) {
			
		String codsiniestro = codigo;
		String codpoliza = pol;
		
		String tiraSQL = "SELECT * FROM siniestro";
		ResultSet resultSet = Conexion.consultar(tiraSQL);		
		try {
			while (resultSet.next()) {
				
				
				String sin_cod = resultSet.getString("sin_cod");
				String sin_pol = resultSet.getString("sin_pol");
			
				if (codsiniestro.equalsIgnoreCase(sin_cod) && codpoliza.equalsIgnoreCase(sin_pol)){
					
					String tiraSQLL = "DELETE FROM siniestro "+
			 				 "WHERE sin_cod = '" + sin_cod +"' and sin_pol = '" +  sin_pol + "'";

					Conexion.ejecutar(tiraSQLL);
					break;
				}
								
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	
	
	
}
